/* ************************************************************************

   Copyright:

   License:

   Authors: SALMON Charles

************************************************************************ */

/* ************************************************************************

#require(bip.thirdParty.jsonrpc)

************************************************************************ */

/**
 * The rpc manager will create instance of buisiness logic component
 */
qx.Class.define("bip.rpc.RpcManager", 
{
	extend: qx.core.Object,

	type: "singleton",

	/*
	*****************************************************************************
	 EVENTS
	*****************************************************************************
	*/  
	events :
	{
		
		
	},

	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Connect to the JSON-RPC server
	 * 
	 */	
 	construct: function()
	{
		this.base(arguments);
		
		this.__debug = false;
		this.__isCreated = false;
		this.__jsonUrl = location.pathname.substring(1);
		this.__jsonUrl = this.__jsonUrl.substring(0,this.__jsonUrl.indexOf("/")) ;
		if ((qx.core.Environment.get("qx.debug")))
    {
			// Dev
			if (! this.__jsonUrl) {
				this.__jsonUrl = "/JSON-RPC";
			} else {
				this.__jsonUrl = "/"+this.__jsonUrl+"/JSON-RPC";
			}
    } else {
    	// Prod
    	this.__jsonUrl = "/JSON-RPC";
    }
		
		//Try to connect to the rpc servlet
		try {
			this.info("Json URL:"+this.__jsonUrl);
			this.__jsonrpc = new JSONRpcClient(this.__jsonUrl);
			//dealing with session timeout
			bip.thirdParty.jsonrpc.setManager(this);
			/*bip.thirdParty.jsonrpc.sessionTimer.setInterval(1000*60*20);  // Recall the user that session is about to expire every 20 minutes
			bip.thirdParty.jsonrpc.sessionTimer.start();
			bip.thirdParty.jsonrpc.sessionTimer.addListener("interval", function() {
				this.info("Session refresh");
				this.__jsonrpc.Session.refreshSession();			
			},this );*/
			this.__isCreated = true;
		} catch(e) {
			this.fatalError(e,"Could not create json rpc ");
		}
	},

	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members:
	{	
		/**
		 * Cancel a request that have been dispatched
		 * @param reqId {Integer} The request id 
		 */
		cancelRequest: function(reqId) {
			JSONRpcClient.cancelRequest(reqId);
		},
		
		/**
		 * Close the session
		 */
		closeSession: function() {
			this.__jsonrpc.Session.invalidateSession();	
		},
		
		/**
		 * Get the name of tue session user
		 */
		getUserName: function() {
			return this.__jsonrpc.Session.getUsername();	
		},
		
		/**
		 * Return the Json Rpc object
		 */
		getJsonRpc: function() {
			return this.__jsonrpc;
		},
		
		/**
		 * Return a new object that is mapped to an existing instance on server side
		 * @param objectId {Integer} 
		 */
		newProxyObject: function(objectId) {
			return new JSONRpcClient(this.__jsonUrl,null,null,objectId);
		},
		
		/*
		*****************************************************************************
		 ERRORS HANDLING
		*****************************************************************************
		*/	
		/**
		 * Will display a fatal error window
		 * 
		 * @param error {Object} The error object
		 * @param title {String} The window title 
		 */
		fatalError: function(error,title) {
			var errorMsg = "";
			if (error.msg != null) { errorMsg += error.msg +" ";}
			if (error.message != null) { errorMsg += error.message +" ";}
			alert("FATAL ERROR !\n "+errorMsg);
		}
	}
	
});
